
package com.pp.admin.common.server.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.pp.admin.common.server.PersistenceManagerFactory;

/**
 * The persistent class for the PP_USERS_DOMAINS database table.
 */
@PersistenceCapable( identityType = IdentityType.APPLICATION )
public class UsersDomain extends Base implements Serializable {
	
	private static final long	serialVersionUID	= 1L;
	
	public static final byte	STATUS_REQUESTED	= 1;
	
	public static final byte	STATUS_APPROVED		= 2;
	
	public static final byte	STATUS_ACTIVE		= 3;
	
	public static final byte	STATUS_DELETED		= 4;
	
	@PrimaryKey
	@Persistent
	private Key					id;
	
	@Persistent
	private Key					userKey;
	
	@Persistent
	private byte				superUser;
	
	@Persistent
	private byte				status;
	
	@Persistent
	private Key					createdBy;
	
	@Persistent
	private Date				createdByTime;
	
	@Persistent
	private Key					lastModefiedBy;
	
	@Persistent
	private Date				lastModefiedByTime;
	
	// bi-directional many-to-one association to Domain
	@Persistent
	private Domain				domain;
	
	// uni-directional many-to-many association to Role
	@Persistent
	private Set<Key>			roleKeys			= new HashSet<Key>();
	
	public UsersDomain() {

	}
	
	/**
	 * @return the id
	 */
	public Key getId() {

		return id;
	}
	
	/**
	 * @param id
	 *            the id to set
	 */
	public void setId( Key id ) {

		this.id = id;
	}
	
	/**
	 * @return the userKey
	 */
	public Key getUserKey() {

		return userKey;
	}
	
	/**
	 * @param userKey
	 *            the userKey to set
	 */
	public void setUserKey( Key userKey ) {

		this.userKey = userKey;
	}
	
	/**
	 * @return the superUser
	 */
	public byte getSuperUser() {

		return superUser;
	}
	
	/**
	 * @param superUser
	 *            the superUser to set
	 */
	public void setSuperUser( byte superUser ) {

		this.superUser = superUser;
	}
	
	/**
	 * @return
	 */
	public boolean isSuperUser() {

		return this.superUser == BOOLEAN_TRUE;
	}
	
	/**
	 * @return the status
	 */
	public byte getStatus() {

		return status;
	}
	
	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus( byte status ) {

		this.status = status;
	}
	
	/**
	 * @return the domain
	 */
	public Domain getDomain() {

		return domain;
	}
	
	/**
	 * @param domain
	 *            the domain to set
	 */
	public void setDomain( Domain domain ) {

		this.domain = domain;
	}
	
	/**
	 * @return the roleKeys
	 */
	public Set<Key> getRoleKeys() {

		return roleKeys;
	}
	
	/**
	 * @param roleKeys
	 *            the roleKeys to set
	 */
	public void setRoleKeys( Set<Key> roleKeys ) {

		this.roleKeys = roleKeys;
	}
	
	/**
	 * @return the createdBy
	 */
	@Override
	public User getCreatedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			user = pm.getObjectById( User.class, this.createdBy );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}
	
	/**
	 * @param createdBy
	 *            the createdBy to set
	 */
	@Override
	public void setCreatedBy( Key createdBy ) {

		this.createdBy = createdBy;
	}
	
	/**
	 * @return the createdByTime
	 */
	@Override
	public Date getCreatedByTime() {

		return createdByTime;
	}
	
	/**
	 * @param createdByTime
	 *            the createdByTime to set
	 */
	@Override
	public void setCreatedByTime( Date createdByTime ) {

		this.createdByTime = createdByTime;
	}
	
	/**
	 * @return the lastModefiedBy
	 */
	@Override
	public User getLastModefiedBy() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			user = pm.getObjectById( User.class, this.lastModefiedBy );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}
	
	/**
	 * @param lastModefiedBy
	 *            the lastModefiedBy to set
	 */
	@Override
	public void setLastModefiedBy( Key lastModefiedBy ) {

		this.lastModefiedBy = lastModefiedBy;
	}
	
	/**
	 * @return the lastModefiedByTime
	 */
	@Override
	public Date getLastModefiedByTime() {

		return lastModefiedByTime;
	}
	
	/**
	 * @param lastModefiedByTime
	 *            the lastModefiedByTime to set
	 */
	@Override
	public void setLastModefiedByTime( Date lastModefiedByTime ) {

		this.lastModefiedByTime = lastModefiedByTime;
	}
	
	public User getUser() {

		User user = null;
		PersistenceManager pm = PersistenceManagerFactory.getPersistenceManager();
		try {
			user = pm.getObjectById( User.class, this.userKey );
		} finally {
			PersistenceManagerFactory.closePersistenceManager();
		}
		return user;
	}
	
	public static Key generateKey( String domainID, String userID ) {

		Key domainKey = Domain.generateKey( userID, domainID );
		return new KeyFactory.Builder( domainKey ).addChild( UsersDomain.class.getSimpleName(), domainID + ":" + userID ).getKey();
	}
	
}
